package com.aoip.telemetry.telemetry.receive.util;

import com.aoip.telemetry.telemetry.core.crc.SnStCrcUtil;

/**
 * @author chenbai
 * @version 1.0
 * @date 2022/7/7 20:21
 */
public class ConfirmUtil {

    public static String generateConfirm(String originalMessage){
        StringBuilder sb = new StringBuilder();
        int length = originalMessage.length();
        int st = originalMessage.indexOf("ST");
        int stCodeBeginIndex = st + 3;
        int stCodeEndIndex = originalMessage.indexOf(" " , 3);
        String stCode = originalMessage.substring(stCodeBeginIndex, stCodeEndIndex);
        sb.append(originalMessage, 0, 3);
        sb.append(stCode);
        if (length - stCodeEndIndex == 5) {
//            心跳包
            String frameString = sb.toString();
            String cs = SnStCrcUtil.crc(frameString);
            sb.append(" ").append(cs).append("NN");
        } else {
            sb.append(" DC ");
            int dc = originalMessage.indexOf("DC ");
            if (dc < 0) {
                return "";
            }
            String num = originalMessage.substring(dc + 3, length - 5);
            String frameString = sb.append(num).append(" OK ").toString();
            String cs = SnStCrcUtil.crc(frameString);
            sb.append(cs).append("NN");
        }
        return sb.toString();
    }
}
